
#import "XMTabBarView.h"
#import "XMTabBarItemView.h"

@interface XMTabBarView ()
@property (nonatomic, strong) NSMutableArray *itemsArray;
@property (nonatomic, strong) XMTabBarItemView *currentItemView;
@property (nonatomic, strong) XMTabBarItemView *badgeItemView;
@end


@implementation XMTabBarView


- (instancetype)init {
    self = [super init];
    NSArray *title = @[@"首页", @"探索", @"消息", @"我的"];
    NSArray *normalImg = @[@"ic_tab_home_n", @"ic_tab_find_n", @"ic_tab_msg_n", @"ic_tab_mine_n"];
    NSArray *selectImg = @[@"ic_tab_home_s", @"ic_tab_find_s", @"ic_tab_msg_s", @"ic_tab_mine_s"];
    
    if (self) {
        [title enumerateObjectsUsingBlock:^(NSString *_Nonnull title, NSUInteger idx, BOOL *_Nonnull stop) {
            XMTabBarItemView *tabBarItem = [[XMTabBarItemView alloc] init];
            tabBarItem.title = title;
            tabBarItem.titleFont = kUIFont(kIPhoneX ? 15 : 13);
            tabBarItem.titleColor = kHEX(@"#989898");
            tabBarItem.titleSelectedColor = kAppMainColor;
            tabBarItem.image = kGetImage(normalImg[idx]);
            tabBarItem.imageSelected = kGetImage(selectImg[idx]);
            tabBarItem.imageSize = CGSizeMake(kIPhoneX ? 26 : 22, kIPhoneX ? 26 : 22);
            XMWeakSelf(self);
            tabBarItem.clickBlock = ^{
                XMStrongSelf(self);
                [self swithItemViewIndex:idx];
            };
            [self.itemsArray addObject:tabBarItem];
        }];
        self.currentItemView = [self.itemsArray firstObject];
        self.badgeItemView = [self.itemsArray objectAtIndex:2];
        
        self.currentItemView.selected = YES;
        [self setupSubviews];
    }
    return self;
}

#pragma mark - setupSubviews
- (void)setupSubviews {
    float itemWidth = kScreenWidth / self.itemsArray.count;
    XMTabBarItemView *lastItemView;
    for (int i = 0; i < self.itemsArray.count; i++) {
        XMTabBarItemView *itemView = [self.itemsArray objectAtIndex:i];
        [self addSubview:itemView];
        [itemView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.bottom.equalTo(self);
            make.width.mas_equalTo(itemWidth);
            if (i == 0) {
                make.left.equalTo(self);
            } else if (lastItemView) {
                make.left.equalTo(lastItemView.mas_right);
            }
        }];
        lastItemView = itemView;
    }
}

- (void)swithItemViewIndex:(NSInteger)index {
    XMTabBarItemView *clickItemView = [self.itemsArray objectOrNilAtIndex:index];
    if (_currentItemView != clickItemView) {
        _currentItemView = clickItemView;
        for (XMTabBarItemView *itemView in self.itemsArray) {
            itemView.selected = (itemView == _currentItemView);
        }
        if (self.switchTabBar) {
            self.switchTabBar(index);
        }
    } else {
        if (self.repeatSwitchTabBar) {
            self.repeatSwitchTabBar(index);
        }
    }
    
}

- (void)swithItemViewIndexNoBlock:(NSInteger)index {
    XMTabBarItemView *clickItemView = [self.itemsArray objectOrNilAtIndex:index];
    if (_currentItemView != clickItemView) {
        _currentItemView = clickItemView;
        for (XMTabBarItemView *itemView in self.itemsArray) {
            itemView.selected = (itemView == _currentItemView);
        }
    }
}

- (void)setBadgeNum:(NSUInteger)badgeNum {
    self.badgeItemView.badgeNum = badgeNum;
}

- (NSMutableArray *)itemsArray {
    if (!_itemsArray) {
        _itemsArray = [[NSMutableArray alloc] init];
    }
    return _itemsArray;
}

@end
